package com.wehealth;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.umeng.analytics.MobclickAgent;
import com.umeng.analytics.a;
import com.wehealth.adapter.Adapter_message;
import com.wehealth.adapter.DrListAdapter;
import com.wehealth.dm.DM_Messages;
import com.wehealth.dm.Doctor;
import com.wehealth.parse.ParseJson;
import com.wehealth.util.DoTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

@SuppressLint("ValidFragment")
public class Dr_Info extends Fragment {

	//SIMMON_ADD
	private static  String JSONResult;
	private Doctor mDocktor;
	private ImageView mDocktorIcon;
	private static final int ONLINE_CONSULT  = 0;
	private TextView doctor_intro;
	private String ID ="-1"; 
	private MainActivity context;
    private AlertDialog dialog;
	
	public  Dr_Info()
	{
		Log.i("init:", "Dr_Info"); 

	}
	
	public  Dr_Info(Doctor docktor,ImageView dorctor_photo){
		this.mDocktor = docktor;
		mDocktorIcon =dorctor_photo;
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

		return  inflater.inflate(R.layout.dr_info, null);
	}
	
	
	
	public void onActivityCreated(Bundle savedInstanceState){
		
		super.onActivityCreated(savedInstanceState);
		

		
		MainActivity activity = (MainActivity)getActivity();
		context = activity;
		TextView title = (TextView)activity.findViewById(R.id.header_title);
		String mTitle = getString(R.string.drinfo_title);
		title.setText(mTitle);
		
		//读取接口数据
		new Thread(){
			@Override
			public void run(){
			//你要执行的方法
				try {
					InitMessageListData();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			//执行完毕后给handler发送一个空消息
			handler.sendEmptyMessage(0);
			}
			}.start();
		MobclickAgent.openActivityDurationTrack(false);
		//返回主页按钮-------------------------------------------------------//
		 Button bBack = (Button) activity.findViewById(R.id.header_bBack);
			
		 bBack.setOnClickListener(new View.OnClickListener()
			{
				public void onClick(View v)
				{
						MainActivity activity = (MainActivity) getActivity();						
						activity.switchContentHome();
				}
			});
			
		//------------------------------------------------------------------//
		 
		 //SIMMON_ADD

		 
		 TextView doctor =(TextView)activity.findViewById(R.id.doctor_name);
		 String doctor_name = mDocktor.getRealName();
		 doctor.setText(doctor_name);

		 TextView doctor_level =(TextView)activity.findViewById(R.id.doctor_level);
		 String doctor_level_string = mDocktor.getLevel();
		 doctor_level.setText(doctor_level_string);
		 
		 TextView doctor_hospital = (TextView)activity.findViewById(R.id.hospital);
		 String doctor_hospital_info = mDocktor.getHospital();
		 doctor_hospital.setText(doctor_hospital_info);
		 
		  doctor_intro =(TextView)activity.findViewById(R.id.dr_intro);
		 String doctor_intro_info= mDocktor.getIntro();
		 doctor_intro.setText(doctor_intro_info);
		 
		 ImageView doctor_face =(ImageView)activity.findViewById(R.id.face_dr);
		Drawable face_drawable= mDocktorIcon.getDrawable();
		 doctor_face.setImageDrawable(face_drawable);
		 
		 ImageButton online_ask=(ImageButton)activity.findViewById(R.id.online_ask_button);
		 
		 online_ask.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				
				ConsultationQestion mConsultation = new ConsultationQestion(mDocktor,ONLINE_CONSULT);
				
				if (getActivity() instanceof MainActivity)
				{
					MainActivity activity = (MainActivity) getActivity();
					
					activity.switchContent(mConsultation);
				}
			}
		});
		 
		 ImageButton online_tel =(ImageButton)activity.findViewById(R.id.online_tel_bt);
		 online_tel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				openDialog();
			}
		});
		 
		 ImageView  self_dr =(ImageView)activity.findViewById(R.id.imageView5);
		 self_dr.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				openDialog();
			}
		});
		 		 
	}
	
	
	private void openDialog(){
	    AlertDialog.Builder builder = new AlertDialog.Builder(context); 
	    String result_str ="功能暂时没有开通！";
	    String okBtBtStr ="知道了";
       View view = LayoutInflater.from(context).inflate(R.layout.dialog_self_viewbt,null);
       TextView textview = (TextView)view.findViewById(R.id.dialog_self_tx);
       textview.setText(result_str);
       Button okBt =(Button)view.findViewById(R.id.dialog_self_bt);
       okBt.setText(okBtBtStr);
       okBt.setOnClickListener(new OnClickListener() {
           
           @Override
           public void onClick(View arg0) {
               // TODO Auto-generated method stub
               dialog.cancel();
           }
       });
        builder.setView(view);
        dialog=  builder.create();
        dialog.show();
	}
	
	//SIMMON_ADD
	
	private void  InitMessageListData() throws IOException
	{
		 String urlAPI = com.wehealth.util.GetUrl.Doctor_InfoUrl();
		 HttpPost httpRequest= new HttpPost(urlAPI);
		 List <NameValuePair> params = new ArrayList <NameValuePair>(); 
		 //将来这个数据从其他界面传过来
		  ID = String.valueOf(mDocktor.getID()) ;
		 params.add(new BasicNameValuePair("id",ID));
		 try
		 {
			 httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8) );
			 HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);	 
			 if(httpResponse.getStatusLine().getStatusCode() == 200)
			 {
				 JSONResult = EntityUtils.toString(httpResponse.getEntity());
			 }
		 }
		 catch(Exception e)
		 {
			 Log.e("error:",  e.toString());		 
		 }

	}
	
	//定义Handler对象
		private Handler handler =new Handler(){

			@Override
			public void handleMessage(android.os.Message msg) {
				// TODO Auto-generated method stub
				super.handleMessage(msg);
				if(JSONResult!=null){
				    BindList(JSONResult);
				}else{
				    context.openMainActivityDialog(); 
				}
			}
			
		};
		

		
		//将JSON数据绑定到LISTVIEW
		private void BindList(String strJSON)
		{
			try 
			{  
			    try
			    {
				    JSONObject jsonObject = new JSONObject(strJSON);		    
				    JSONArray  aNews =  new JSONArray( jsonObject.getString("result") ); 
				    String info;
				    for(int i=0; i<aNews.length(); i++){
				    	String id = aNews.getJSONObject(i).getString("ID");
				    	   if(id.equals(ID)){
				    		   info = aNews.getJSONObject(i).getString("intro");
				    		   doctor_intro.setText(info);
				    	   }
				    }
			        //SIMMON_ADD end
			    }
			    catch(JSONException ex)
			    {
			    		Log.e("JSON Error: ", ex.toString()); 
			    }
			} 
			catch (Exception ex) 
			{  
			    
				Log.e("error: ",  ex.toString());
			}
		}

	    @Override
	    public void onPause() {
	        // TODO Auto-generated method stub
	        super.onPause();
	        MobclickAgent.onPageEnd("Dr_ifo"); 
	    }


	    @Override
	    public void onResume() {
	        // TODO Auto-generated method stub
	        super.onResume();
	        MobclickAgent.onPageStart("Dr_ifo"); 
	    }

}
